package com.runen.infinitemovement.widget.popupwind;


import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.runen.infinitemovement.Constant;
import com.runen.infinitemovement.R;
import com.runen.infinitemovement.activity.WebActivity;
import com.runen.infinitemovement.model.api.UserAPI;
import com.runen.infinitemovement.tool.GlideUtils;

/**
 * Create by LiaoDuanHong
 * Create on
 */
public class ReceiveAwardPopupWindow extends PopupWindow {
    public Context context;
    private View layout;

    private String prizeName;
    private String prizeUrl;
    private String prizeTitle;
    private String pathId;
    private String sectionId;

    private TextView tv_message;
    private TextView tv_prize_name;
    private TextView tv_receive_award;
    private ImageView iv_content;
    private ImageView iv_dismiss;

    public static ReceiveAwardPopupWindow getInstance(Context context,
                                                      String prizeName,
                                                      String prizeUrl,
                                                      String prizeTitle,
                                                      String pathId,
                                                      String sectionId) {
        return new ReceiveAwardPopupWindow(context, prizeName, prizeUrl, prizeTitle, pathId, sectionId);
    }

    private ReceiveAwardPopupWindow(Context context, String prizeName,
                                    String prizeUrl,
                                    String prizeTitle,
                                    String pathId,
                                    String sectionId) {
        super(context);
        this.context = context;
        this.prizeName = prizeName;
        this.prizeUrl = prizeUrl;
        this.prizeTitle = prizeTitle;
        this.pathId = pathId;
        this.sectionId = sectionId;
        initView();
        this.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        //this.setHeight((int) (CommonUtil.getScreenHW((Activity) context)[1] / 3));
        this.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        this.setFocusable(true);
        this.setOutsideTouchable(true);
        // 这样设置才能点击屏幕外dismiss窗口
        this.setBackgroundDrawable(new BitmapDrawable());
        this.setContentView(layout);

        initEvent();
    }

    private void initView() {
        layout = LayoutInflater.from(context).inflate(R.layout.view_custom_receive_award, null);
        tv_message = layout.findViewById(R.id.tv_message);
        tv_prize_name = layout.findViewById(R.id.tv_prize_name);
        iv_content = layout.findViewById(R.id.iv_content);
        iv_dismiss = layout.findViewById(R.id.iv_dismiss);
        tv_receive_award = layout.findViewById(R.id.tv_receive_award);
        tv_message.setText(prizeTitle);
        if (prizeName.length() > 6) {
            prizeName = prizeName.substring(0, 5) + "...";
        }
        tv_prize_name.setText("【" + prizeName + "】");
        GlideUtils.loadImage(context, prizeUrl, iv_content);
    }

    private void initEvent() {
        iv_dismiss.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        tv_receive_award.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent1 = new Intent(context, WebActivity.class);
                intent1.putExtra("LINK_URL", String.format(Constant.H5_RECEIVE_AWARD, UserAPI.getUserToken(), pathId, sectionId));
                context.startActivity(intent1);
            }
        });
    }

    public void show() {
//        this.setAnimationStyle(R.style.Animation);
        //防止部分手机底部状态栏与popupWindow重叠
        this.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
        this.setAnimationStyle(R.style.AnimationBottomFade);
        //popupWindow底部显示
        this.showAtLocation(((Activity) context).getWindow().getDecorView().findViewById(android.R.id.content), Gravity.CENTER, 0, 0);
        WindowManager.LayoutParams lp = ((Activity) context).getWindow().getAttributes();
        lp.alpha = 0.3f;
        if (lp.alpha == 1) {
            ((Activity) context).getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);//不移除该Flag的话,在有视频的页面上的视频会出现黑屏的bug
        } else {
            ((Activity) context).getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);//此行代码主要是解决在华为手机上半透明效果无效的bug
        }
        ((Activity) context).getWindow().setAttributes(lp);
    }

    @Override
    public void dismiss() {
        super.dismiss();
        WindowManager.LayoutParams lp = ((Activity) context).getWindow().getAttributes();
        lp.alpha = 1f;
        ((Activity) context).getWindow().setAttributes(lp);
    }
}
